﻿

local moduleName = "Dispel"



----------------------------
--      Localization      --
----------------------------

local L = {
	[moduleName] = "Dispel",

	["Debuffs"] = true,
	["Enable/Disable debuffs."] = true,

	["Add debuff"] = true,
	["Add a debuff to show. Must match the exact name of the debuff, including capital case."] = true,

	["Sound Warning"] = true,
	["Play a sound when there is something to dispel."] = true,

	["Side"] = true,
	["Which side to position debuff icons at."] = true,
	["Left"] = true,
	["Right"] = true,

	["Text Color"] = true,
	["Change the text color."] = true,

	["Highlight"] = true,
	["Ignore debuff list."] = true,

	["Show All Icons"] = true,
	["Show all debuff Icons"] = true,

	["Enable/Disable this debuff. Shift click to remove it completly."] = true,
	["|cffff0000%s|r added to the debuff list."] = true,
	["|cffff0000%s|r removed from the debuff list."] = true,
	["|cffff0000%s|r disenable from the debuff list."] = true,
	["|cffff0000%s|r is in the debuff list."] = true,

	["Show Buffs"] = true,
	["How many buffs will show"] = true,

	["Cooldown Shadow"] = true,
	["Show shadow on buff icon."] = true,
	
	["Cooldown Text"] = true,
	["Show text on buff icon."] = true,

	["DeBuff Tooltip"] = true,
	["Show debuff tooltips."] = true,
}
if GetLocale() == "zhCN" then
	L[moduleName] = "Debuff-减益效果"

	L["Debuffs"] = "Debuff列表"
	L["Enable/Disable debuffs."] = "选择/不选择 要一直高亮显示的减益效果"

	L["Add debuff"] = "添加减益效果"
	L["Add a debuff to show. Must match the exact name of the debuff, including capital case."] = "新添加一个减益效果到Debuff列表中.\n必须输入正确的减益效果名称, 否则不会有效果"

	L["Sound Warning"] = "音效提示"
	L["Play a sound when there is something to dispel."] = "需要驱散时播放音效"

	L["Text Color"] = "文字颜色"
	L["Change the text color."] = "变更文字颜色"

	L["Highlight"] = "高亮全部Debuff"
	L["Ignore debuff list."] = "启用,后将高亮所有Debuff,不能解的全部显示为红色\n禁用,只高亮能解的Debuff和Debuffs列表中的\n在Debuff列表中的如果不能解也将高亮为红色"

	L["Show All Icons"] = "显示全部Debuff图标"
	L["Show all debuff Icons"] = "显示所有Debuff图标,禁用则只显示能解的Debuff图标和Debuff列表中的"

	L["Enable/Disable this debuff. Shift click to remove it completly."] = "选择/不选择 要显示的减益效果. Shift+点击 删除"
	L["|cffff0000%s|r added to the debuff list."] = "|cffff0000%s|r 添加到减益效果列表中"
	L["|cffff0000%s|r removed from the debuff list."] = "|cffff0000%s|r 从减益效果列表中删除"
	L["|cffff0000%s|r disenable from the debuff list."] = "|cffff0000%s|r 从减益效果列表中禁用"
	L["|cffff0000%s|r is in to the debuff list."] = "|cffff0000%s|r 已经在减益效果列表中"

	L["Show Buffs"] = "显示DeBuff数量"
	L["How many buffs will show"] = "显示DeBuff数量"

	L["Side"] = "位置"
	L["Which side to position debuff icons at."] = "设置在哪一边放置减益法术效果图标。"
	L["Left"] = "左"
	L["Right"] = "右"

	L["Cooldown Shadow"] = "CD阴影"
	L["Show shadow on buff icon."] = "阴影显示CD"
	
	L["Cooldown Text"] = "CD文字"
	L["Show text on buff icon."] = "文字显示CD"

	L["DeBuff Tooltip"] = "Debuff信息"
	L["Show debuff tooltips."] = "显示debuff的信息"
elseif GetLocale() == "zhTW" then
	L[moduleName] = "Debuff-減益效果"

	L["Debuffs"] = "Debuff列表"
	L["Enable/Disable debuffs."] = "選擇/不選擇 要一直高亮顯示的減益效果"

	L["Add debuff"] = "添加減益效果"
	L["Add a debuff to show. Must match the exact name of the debuff, including capital case."] = "新添加一個減益效果到Debuff列表中.\n必須輸入正確的減益效果名稱, 否則不會有效果"

	L["Sound Warning"] = "音效提示"
	L["Play a sound when there is something to dispel."] = "需要驅散時播放音效"

	L["Text Color"] = "文字顏色"
	L["Change the text color."] = "變更文字顏色"

	L["Highlight"] = "高亮全部Debuff"
	L["Ignore debuff list."] = "啟用,後將高亮所有Debuff,不能解的全部顯示為紅色\n禁用,只高亮能解的Debuff和Debuffs列表中的\n在Debuff列表中的如果不能解也將高亮為紅色"

	L["Show All Icons"] = "顯示全部Debuff圖示"
	L["Show all debuff Icons"] = "顯示所有Debuff圖示,禁用則只顯示能解的Debuff圖示和Debuff清單中的"

	L["Enable/Disable this debuff. Shift click to remove it completly."] = "選擇/不選擇 要顯示的減益效果. Shift+點擊 刪除"
	L["|cffff0000%s|r added to the debuff list."] = "|cffff0000%s|r 添加到減益效果列表中"
	L["|cffff0000%s|r removed from the debuff list."] = "|cffff0000%s|r 從減益效果列表中刪除"
	L["|cffff0000%s|r disenable from the debuff list."] = "|cffff0000%s|r 從減益效果列表中禁用"
	L["|cffff0000%s|r is in to the debuff list."] = "|cffff0000%s|r 已經在益效果列表中"

	L["Show Buffs"] = "顯示DeBuff數量"
	L["How many buffs will show"] = "顯示DeBuff數量"

	L["Side"] = "位置"
	L["Which side to position debuff icons at."] = "設置在哪一邊放置減益法術效果圖示。"
	L["Left"] = "左"
	L["Right"] = "右"

	L["Cooldown Shadow"] = "CD陰影"
	L["Show shadow on buff icon."] = "陰影顯示CD"
	
	L["Cooldown Text"] = "CD文字"
	L["Show text on buff icon."] = "文字顯示CD"

	L["DeBuff Tooltip"] = "Debuff信息"
	L["Show debuff tooltips."] = "顯示debuff的資訊"
end


----------------------------------
--      Local Declaration      --
----------------------------------

local candispel = {
	["WARRIOR"] = { },
	["ROGUE"] = { },
	["HUNTER"] = { },
	["MAGE"] = {
		["Curse"] = true
	},
	["WARLOCK"] = { },
	["DRUID"] = { 
		["Curse"] = true,
		["Poison"] = true
	},
	["PALADIN"] = {
		["Magic"] = true,
		["Poison"] = true,
		["Disease"] = true
	},
	["PRIEST"] = {
		["Magic"] = true,
		["Disease"] = true
	},
	["SHAMAN"] = {
		["Disease"] = true,
		["Poison"] = true,
		["Curse"] = false
	},
	["DEATHKNIGHT"] = { }
}

do
	local class = select(2, UnitClass("player"))
	candispel = candispel[class] -- let's hope the rest of the candispel table is garbage collected after this
	candispel["none"] = true
	if class == "SHAMAN" then
		local _, _, _, _, rank = GetTalentInfo(3, 18)
		if rank == 1 then
			candispel["Curse"] = true
		end
	end
end

local positions ={
	["LEFT"] = L["Left"],
	["RIGHT"] = L["Right"]
}
local iconsize = 19

local ignore_id = {
	-- debuffs here should never be dispelled
	["All"] = {
		15822, -- Dreamless Sleep
		24360, -- Greater Dreamless Sleep
		28504, -- Major Dreamless Sleep

		24306, -- Delusions of Jin'do

		30108, -- Unstable Affliction

		42966 -- Upset Tummy because it's very annoying
	},
	-- debuffs here can be ignored
	["WARRIOR"] = {
		19372, -- Ancient Hysteria Spirit reduced by 50%, Intellect reduced by 50%.
		19659, -- Ignite Mana 400 mana burned every 3 sec.
		16567, -- Tainted Mind Intellect reduced by 20%.
		25195 -- Curse of Tongues Casting speed reduced by 75%.
	},
	["ROGUE"] = {
		19372, -- Ancient Hysteria Spirit reduced by 50%, Intellect reduced by 50%.
		19659, -- Ignite Mana 400 mana burned every 3 sec.
		16567, -- Tainted Mind Intellect reduced by 20%.
		25195, -- Curse of Tongues Casting speed reduced by 75%.
		15487, -- Silence Cannot cast spells.
		8281 -- Sonic Burst Cannot cast spells.
	},
	["HUNTER"] = {
	},
	["MAGE"] = {
		50285, -- Dust Cloud Chance to hit reduced by 30%.
		17105 -- Banshee Curse Reduces an enemy's chance to hit by 10%.
	},
	["WARLOCK"] = {
		50285, -- Dust Cloud Chance to hit reduced by 30%.
		17105 -- Banshee Curse Reduces an enemy's chance to hit by 10%.
	},
	["DRUID"] = {
	},
	["PALADIN"] = {
	},
	["PRIEST"] = {
		50285, -- Dust Cloud Chance to hit reduced by 30%.
		17105 -- Banshee Curse Reduces an enemy's chance to hit by 10%.
	},
	["SHAMAN"] = {
	},
	["DEATHKNIGHT"] = {
		29310 -- Spell Disruption
	}
}
local ignore = {}
for a in pairs(ignore_id) do
	ignore[a] = {}
	for _,id in ipairs(ignore_id[a]) do
		local tmp = GetSpellInfo(id)
		ignore[a][tmp] = true
	end
end


-- we have a local colors table so we can adjust it
local colors = { 
	-- stolen from FrameXML/BuffFrame.lua
	["none"]    	= {1, 0, 0, 1},
	["Magic"]	= {0, 0, 1, 1},
	["Curse"]	= {0.5, 0, 0.5, 1},
	["Disease"]	= {0.55, 0.15, 0, 1},
	["Poison"]	= {0, 1, 0, 1}
}


local sound = "Sound\\Doodad\\BellTollNightElf.wav" -- Simple ding


-- localize these functions to speed up the main loop a bit
local UnitClass = UnitClass
local UnitDebuff = UnitDebuff
local DebuffTypeColor = DebuffTypeColor
local GameTooltip = GameTooltip

local debuffs = {}

local debuffsID = {
	38235, -- Water Tomb - Hydros
	38028, -- Watery Grave - Morogrim
	27808, -- Frost Blast - Kel'thuzad
	37066, -- Garrote - Moroes
	12294, -- Mortal Strike
	37640, -- Whirlwind - Leotheras the Blind
	37676, -- Insidious Whisper - Leotheras the Blind
	38280, -- Static Charge - Lady Vashj
	38258, -- Panic - Lady Vashj Striders
	33040, -- Wrath of the Astromancer - High Astromancer Solarian
	42170, -- Conflagration - Kael'thas Sunstrider
	37027, -- Remote Toy - Kael'thas Sunstrider
	40239, -- Incinerate - Teron Gorefiend
	40251, -- Shadow of Death - Teron Gorefiend
	35410, -- Melt Armor - Al'ar
	41472, -- Divine Wrath - Illidari Council
	41485, -- Deadly Poison - Illidari Council
	41001, -- Fatal Attraction - Mother Shahraz
	40860, -- Vile Beam - Mother Shahraz
	39837, -- Impaling Spine - High Warlord Naj'entus
	31945, -- Doomfire - Archimonde
	41914, -- Parasitic Shadowfiend - Illidan Stormrage
	40932, -- Agonizing Flames - Illidan Stormrage
	41032, -- Shear - Illidan Stormrage
	40585, -- Dark Barrage - Illidan Stormrage
	43095, -- Creeping Paralysis - Zul'jin
	43093, -- Grievous Throw - Zul'jin
	42005, -- Bloodboil - Gurtogg Bloodboil (having one of these debuffs is pretty normal here, 2 or more isn't)
	33527, -- Arcane Buffet
	45029, -- Corrupting Strike
	31900, -- Stomp
	43217, -- Burn
	44883, -- Encapsulate
	45866, -- Corrosion
	45855, -- Gas Nova
	46771, -- Flame Sear
	22574, -- Dark Strike
	45347, -- Dark Touched
	45348, -- Flame Touched
	22206, -- Moonfire
	45641, -- Fire Bloom
	20475, -- Living Bomb
	48210, -- Haunt
	50844, -- Blood Mirror
	53467, -- Locust Swarm
	28673, -- Web Wrap
	54210, -- Rain of Fire
	28169, -- Mutating Injection
	38770, -- Mortal Wound
	28059, -- Positive Charge
	28084, -- Negative Charge
	29232, -- Fungal Creep - actually a buff
	28832, -- Mark of Korth'azz
	28834, -- Mark of Rivendare
	28833, -- Mark of Blaumeux
	28835, -- Mark of Zeliek
	57369, -- Unholy Shadow
	60241, -- Flame Tsunami
	55550, -- Jagged Knife
}
local debuffsName = {}
local debuffsNames = {}

----------------------------------
--      Module Declaration      --
----------------------------------

local plugin = BunchOfBars:NewModule(moduleName, "AceTimer-3.0")
local GameTooltip = GameTooltip
plugin.revision = tonumber(("$Revision: 40 $"):match("%d+"))

plugin.options = {
	name = L[moduleName],
	args = {
		debuffs = {
			type = "multiselect",
			name = L["Debuffs"],
			desc = L["Enable/Disable debuffs."],
			values = {},
			get = "GetSetDebuffs",
			set = "GetSetDebuffs",
			order = 1
		},
		add = {
			type  = "input",
			name  = L["Add debuff"],
			desc  = L["Add a debuff to show. Must match the exact name of the debuff, including capital case."],
			get   = "NewDebuff",
			set   = "NewDebuff",
			order = 2
		},
		sound = {
			type = "toggle",
			name = L["Sound Warning"],
			desc = L["Play a sound when there is something to dispel."],
            		get  = "GetSetSound",
            		set  = "GetSetSound"
		},
		color = {
			type = "color",
			name = L["Text Color"],
			desc = L["Change the text color."],
			get = "GetSetColor",
			set = "GetSetColor"
		},
		icon = {
			type = "toggle",
			name = L["Show All Icons"],
			desc = L["Show all debuff Icons"],
			get = "GetSetIcon",
			set = "GetSetIcon"
		},
		highlight = {
			type = "toggle",
			name = L["Highlight"],
			desc = L["Ignore debuff list."],
			get = "GetSetHighlight",
			set = "GetSetHighlight"
		},
		sbuffs = {
			type = "range",
			name = L["Show Buffs"],
			desc = L["How many buffs will show"],
			min = 1,
            		max = 8,
            		step = 1,
           		get = "GetSetBuffs",
            		set = "GetSetBuffs",
		},
		cdshadow = {
			type  = "toggle",
			name  = L["Cooldown Shadow"],
			desc  = L["Show shadow on buff icon."],
			get = "GetSetCDS",
			set = "GetSetCDS",
		},
--[[		cdtext = {
			type  = "toggle",
			name  = L["Cooldown Text"],
			desc  = L["Show text on buff icon."],
			get = "GetSetCDT",
			set = "GetSetCDT",
		},
--]]
		tooltip = {
			type  = "toggle",
			name  = L["DeBuff Tooltip"],
			desc  = L["Show debuff tooltips."],
			get = "GetSetTooltip",
			set = "GetSetTooltip",
		},
		side = {
			type  = "select",
			name  = L["Side"],
			desc  = L["Which side to position debuff icons at."],
			values = positions,
			get = "GetSetSide",
			set = "GetSetSide",
		},
--[[
		insets = {
			type = "range",
			name = "insets",
			desc = "insets",
			min = -10,
            		max = 25,
            		step = 1,
           		get = "GetSetInsets",
            		set = "GetSetInsets",
		},
		edgeSize = {
			type = "range",
			name = "edgeSize",
			desc = "edgesize",
			min = 1,
            		max = 25,
            		step = 1,
           		get = "GetSetedgeSize",
            		set = "GetSetedgeSize",
		}
--]]
	}
}
plugin.defaultDB = {
	profile = {
		sound   = false,
		color  = {
			r = 1,
			g = 0,
			b = 0
		},
	--	position = 1,
		cdshadow = false,
		cdtext = false,
		sbuffs = 4,
		highlight = false,
		icon = false,
		tooltip = true,
		side = "LEFT",
	--edgeSize = 14,
	--insets = 4,
		debuffs = debuffsNames,
		add = {}
	}
}
for i,a in ipairs(debuffsID) do
	local tmp = GetSpellInfo(a)
	debuffsNames[tmp] = true
end


----------------------------------
--      Module Functions        --
----------------------------------


function plugin:OnEnable()
	self:ScheduleRepeatingTimer(self.UpdateAll, 0.5, self)
	self:List()
end
function plugin:List()
	for i in pairs(debuffsNames) do
		table.insert(debuffsName, i)
	end
	for i,a in pairs(self.db.profile.add) do
		table.insert(debuffsName, i)
	end
	self.options.args.debuffs.values = debuffsName
end

function plugin:OnCreate(frame)
	local debuffbar = CreateFrame("Frame", nil, frame)
	debuffbar:SetWidth(1)
	debuffbar:SetHeight(30)

	local edgeFile = "Interface\\Addons\\BunchOfBars\\Textures\\Caith2"
	local insets = 4 --self.db.profile.insets
	local edgeSize = 16 --self.db.profile.edgeSize
	local border = frame:CreateTexture(nil,"BORDER")
	debuffbar.border1 = border
	border:SetTexture(edgeFile)
	border:SetWidth(edgeSize)
	border:SetHeight(edgeSize)
	border:SetPoint("TOPLEFT", frame, "TOPLEFT", -insets, -edgeSize + insets)
	border:SetPoint("BOTTOMLEFT", frame, "BOTTOMLEFT", -insets, edgeSize - insets)
	border:SetTexCoord(0, 0, 0, 1.948, 0.125, 0, 0.125, 1.948)
	local border = frame:CreateTexture(nil,"BORDER")
	debuffbar.border2 = border
	border:SetTexture(edgeFile)
	border:SetWidth(edgeSize)
	border:SetHeight(edgeSize)
	border:SetPoint("TOPRIGHT", frame, "TOPRIGHT", insets, -edgeSize + insets)
	border:SetPoint("BOTTOMRIGHT", frame, "BOTTOMRIGHT", insets, edgeSize - insets)
	border:SetTexCoord(0.125, 0, 0.125, 1.948, 0.25, 0, 0.25, 1.948)
	local border = frame:CreateTexture(nil,"BORDER")
	debuffbar.border3 = border
	border:SetTexture(edgeFile)
	border:SetWidth(edgeSize*8)
	border:SetHeight(edgeSize)
	border:SetPoint("TOPLEFT", frame, "TOPLEFT", edgeSize - insets, insets)
	border:SetPoint("TOPRIGHT", frame, "TOPRIGHT", -edgeSize + insets, insets)
	border:SetTexCoord(0.25, 9.2808, 0.375, 9.2808, 0.25, 0, 0.375, 0)
	local border = frame:CreateTexture(nil,"BORDER")
	debuffbar.border4 = border
	border:SetTexture(edgeFile)
	border:SetWidth(edgeSize*8)
	border:SetHeight(edgeSize)
	border:SetPoint("BOTTOMLEFT", frame, "BOTTOMLEFT", edgeSize - insets, -insets)
	border:SetPoint("BOTTOMRIGHT", frame, "BOTTOMRIGHT", -edgeSize + insets, -insets)
	border:SetTexCoord(0.375, 9.2808, 0.5, 9.2808, 0.375, 0, 0.5, 0)
	local border = frame:CreateTexture(nil,"BORDER")
	debuffbar.border5 = border
	border:SetTexture(edgeFile)
	border:SetWidth(edgeSize)
	border:SetHeight(edgeSize)
	border:SetPoint("TOPLEFT", frame, "TOPLEFT", -insets, insets)
	border:SetTexCoord(0.5, 0, 0.5, 1, 0.625, 0, 0.625, 1)
	local border = frame:CreateTexture(nil,"BORDER")
	debuffbar.border6 = border
	border:SetTexture(edgeFile)
	border:SetWidth(edgeSize)
	border:SetHeight(edgeSize)
	border:SetPoint("TOPRIGHT", frame, "TOPRIGHT", insets, insets)
	border:SetTexCoord(0.625, 0, 0.625, 1, 0.75, 0, 0.75, 1)
	local border = frame:CreateTexture(nil,"BORDER")
	debuffbar.border7 = border
	border:SetTexture(edgeFile)
	border:SetWidth(edgeSize)
	border:SetHeight(edgeSize)
	border:SetPoint("BOTTOMLEFT", frame, "BOTTOMLEFT", -insets, -insets)
	border:SetTexCoord(0.75, 0, 0.75, 1, 0.875, 0, 0.875, 1)
	local border = frame:CreateTexture(nil,"BORDER")
	debuffbar.border8 = border
	border:SetTexture(edgeFile)
	border:SetWidth(edgeSize)
	border:SetHeight(edgeSize)
	border:SetPoint("BOTTOMRIGHT", frame, "BOTTOMRIGHT", insets, -insets)
	border:SetTexCoord(0.875, 0, 0.875, 1, 1, 0, 1, 1)

	return debuffbar
end

function plugin:CreateDeBuff(debuffbar, n)
	local debuff_frame = CreateFrame("Button", nil, debuffbar)
	debuff_frame:SetHeight(iconsize)
	debuff_frame:SetWidth(iconsize - 1)
	debuff_frame:ClearAllPoints()
	if self.db.profile.side == "RIGHT" then
		debuff_frame:SetPoint("BOTTOMLEFT", debuffbar, "BOTTOMLEFT", (n - 1) * (iconsize + 4), 2) --right side
	elseif self.db.profile.side == "LEFT" then
		debuff_frame:SetPoint("BOTTOMRIGHT", debuffbar, "BOTTOMRIGHT", -((n - 1) * (iconsize + 4)), 2) --left side
	end

	debuff_frame:Hide()
	
	local t = debuff_frame:CreateTexture(nil, "BACKGROUND")
	t:SetVertexColor(1, 1, 1, 0.9)
	t:SetTexCoord(0.2, 0.8, 0.1, 0.9)
	t:ClearAllPoints()
	t:SetAllPoints(debuff_frame)
	t:Show()
	debuff_frame.t = t

	local font = debuff_frame:CreateFontString(nil, "OVERLAY")
	font:SetFont("Interface\\Addons\\BunchOfBars\\Textures\\b.ttf", 18)
	font:SetTextColor(self.db.profile.color.r, self.db.profile.color.g, self.db.profile.color.b, 1)
	font:SetShadowColor(0, 0, 0, 1)
	font:SetShadowOffset(0.8, -0.8)
	font:ClearAllPoints()
	font:SetAllPoints(debuff_frame)
	font:Show()
	debuff_frame.tfont = font

	font = debuff_frame:CreateFontString(nil, "OVERLAY")
	font:SetFont("Interface\\Addons\\BunchOfBars\\Textures\\b.ttf", 18)
	font:SetTextColor(self.db.profile.color.r, self.db.profile.color.g, self.db.profile.color.b, 1)
	font:SetShadowColor(0, 0, 0, 1)
	font:SetShadowOffset(1, -1)
	font:ClearAllPoints()
	font:SetPoint("BOTTOMRIGHT", debuff_frame, "BOTTOMRIGHT", 1, -1)
	font:Show()
	debuff_frame.afont = font


	local cd = CreateFrame("Cooldown", nil, debuff_frame, "CooldownFrameTemplate")
	cd:SetReverse(true)
	cd:SetAllPoints(debuff_frame)
	cd:Hide()
	debuff_frame.cd = cd

	local border = debuff_frame:CreateTexture(nil, "BORDER") --ARTWORK
	border:SetTexture("Interface\\Addons\\BunchOfBars\\Textures\\border")
	border:SetAlpha(1)
	border:ClearAllPoints()
	border:SetPoint("CENTER", debuff_frame, "CENTER", 0, 0)
	border:SetWidth(iconsize + 3)
	border:SetHeight(iconsize + 3)
	border:Show()
	debuff_frame.border = border

	debuffbar[n] = debuff_frame
end

function plugin:OnUpdate(frame)
if not UnitExists(frame.unit) then return end
	local debuffbar = frame.parts[moduleName]
	local n = 1
	local m = 0
	local class = select(2, UnitClass(frame.unit))

--	local hadone = debuffbar.border1:IsShown()

	for i = 1, 8 do
		debuffbar["border"..i]:Hide()
	end

	for k in pairs(debuffs) do debuffs[k] = nil end -- TODO: debuffs is a local variable which seems to decrease memory usage by a lot, verify this.


	for i = 1,40 do
		local name, _, texture, applications, type, duration, timeleft = UnitDebuff(frame.unit, i)

		if not name or (n > self.db.profile.sbuffs) then break end

		if candispel[type] then
			if ignore["All"][name] then
				debuffs[type] = "nodispel"
			elseif not ignore[class][name] and debuffs[type] ~= "nodispel" then
				debuffs[type] = name
			end
		elseif self.db.profile.debuffs[name] or self.db.profile.highlight then
			debuffs["none"] = name
		end

		if self.db.profile.icon or candispel[type] or self.db.profile.debuffs[name] then
			if not debuffbar[n] then
				self:CreateDeBuff(debuffbar, n)
			end

			debuffbar[n].t:SetTexture(texture)

			if self.db.profile.tooltip then
				debuffbar[n]:SetScript("OnEnter",function(self)
					GameTooltip:SetOwner(debuffbar, "ANCHOR_CURSOR")
--[[					GameTooltip.owner = debuffbar
					if UnitIsUnit(frame.unit, "player") then
						local id = GetPlayerBuff(i, "HARMFUL")
						GameTooltip:SetPlayerBuff(id, "HARMFUL")
						GameTooltip:Show()
					else
						GameTooltip:SetUnitDebuff(frame.unit, i)
					end
--]]
					if frame.unit then
						GameTooltip:SetUnitDebuff(frame.unit, i)
					end
				end)
				debuffbar[n]:SetScript("OnLeave",function(self)
					GameTooltip:Hide()
				end)
			end

			if type then
				debuffbar[n].border:SetVertexColor(unpack(colors[type]))
			else
				debuffbar[n].border:SetVertexColor(unpack(colors["none"]))				
			end

			if duration and duration > 0 and self.db.profile.cdshadow then
				debuffbar[n].cd:SetCooldown(timeleft - duration, duration)
				debuffbar[n].cd:Show()
			else
				debuffbar[n].cd:Hide()
			end
--[[
			if self.db.profile.cdtext and timeleft and timeleft > 0 then
				if timeleft > 60 then timeleft = timeleft / 60 end
				timeleft = math.floor(timeleft)

				debuffbar[n].tfont:SetText(timeleft)
			else
				debuffbar[n].tfont:SetText()
			end
--]]
			if applications > 1 then
				debuffbar[n].afont:SetText(applications)
			else
				debuffbar[n].afont:SetText()
			end

			debuffbar[n]:Show()

			n = n + 1
		end
	end
	debuffbar:SetWidth((n - 1) * (iconsize + 3) + 1)
	while debuffbar[n] do
		debuffbar[n]:Hide()
		debuffbar[n].t:SetTexture()
		debuffbar[n]:SetScript("OnEnter", nil)
		debuffbar[n]:SetScript("OnLeave", nil)
		debuffbar[n].tfont:SetText()
		debuffbar[n].afont:SetText()
		n = n + 1
	end

	for type in pairs(candispel) do
		if debuffs[type] and debuffs[type] ~= "nodispel" then
			for i = 1, 8 do
				debuffbar["border"..i]:SetVertexColor(unpack(colors[type]))
				debuffbar["border"..i]:Show()
			end

			if not hadone and self.db.profile.sound and (GetCVar("Sound_EnableSFX") == 1) then
				PlaySoundFile(sound)
			end
			return
		end
	end

end

----------------------------------
--      Option Handlers         --
----------------------------------
function plugin:GetSetDebuffs(_, n, v)
	if type(v) == "nil" then return self.db.profile.debuffs[debuffsName[n]] end

	if IsShiftKeyDown() then
		if plugin.defaultDB.profile.debuffs[debuffsName[n]] then
			self.db.profile.debuffs[debuffsName[n]] = false
	
			self.core:Print(string.format(L["|cffff0000%s|r disenable from the debuff list."], debuffsName[n]))
		else
			self.db.profile.add[debuffsName[n]] = nil
			self.db.profile.debuffs[debuffsName[n]] = nil
			self.core:Print(string.format(L["|cffff0000%s|r removed from the debuff list."], debuffsName[n]))
			debuffsName[n] = nil
		end

	else
		self.db.profile.debuffs[debuffsName[n]] = v
	end

end
function plugin:NewDebuff(_, n)
	if type(n) == "nil" then return "" end
	local add = true
	n = strtrim(n)
	for i,k in pairs(debuffsName) do
		if k == n then
			add = false
			break
		end
	end
	if add then
		self.db.profile.add[n] = true
		self.core:Print(string.format(L["|cffff0000%s|r added to the debuff list."], n))
		self.db.profile.debuffs[n] = true
		table.insert(debuffsName, n)
	else
		self.core:Print(string.format(L["|cffff0000%s|r is in the debuff list."], n))
	end

end


function plugin:GetSetSound(_, v)
	if type(v) == "nil" then return self.db.profile.sound end

	self.db.profile.sound = v

	if v then PlaySoundFile(sound) end
end

function plugin:GetSetIcon(_, v)
	if type(v) == "nil" then return self.db.profile.icon end

	if self.db.profile.icon ~= v then
		self.db.profile.icon = v

		self:UpdateAll()
	end
end

function plugin:GetSetHighlight(_, v)
	if type(v) == "nil" then return self.db.profile.highlight end

	if self.db.profile.highlight ~= v then
		self.db.profile.highlight = v
	end
end

function plugin:GetSetColor(_, r, g, b)
	if type(r) == "nil" then return self.db.profile.color.r, self.db.profile.color.g, self.db.profile.color.b end

	self.db.profile.color.r = r
	self.db.profile.color.g = g
	self.db.profile.color.b = b

	for _,frame in pairs(self.core.frames) do
		local debuffbar, n = frame.parts[moduleName], 1

		while debuffbar[n] do
			debuffbar[n].afont:SetTextColor(self.db.profile.color.r ,self.db.profile.color.g, self.db.profile.color.b, 1)
			debuffbar[n].tfont:SetTextColor(self.db.profile.color.r ,self.db.profile.color.g, self.db.profile.color.b, 1)
			n = n + 1
		end
	end
end

function plugin:GetSetBuffs(_, v)
	if type(v) == "nil" then return self.db.profile.sbuffs end

	if self.db.profile.sbuffs ~= v then
		self.db.profile.sbuffs = v
		self:UpdateAll()
	end
end

function plugin:GetSetCDS(_, v)
	if type(v) == "nil" then return self.db.profile.cdshadow end

	if self.db.profile.cdshadow ~= v then
		self.db.profile.cdshadow = v

		self:UpdateAll()
	end
end

function plugin:GetSetCDT(_, v)
	if type(v) == "nil" then return self.db.profile.cdtext end

	if self.db.profile.cdtext ~= v then
		self.db.profile.cdtext = v

		self:UpdateAll()
	end
end

function plugin:GetSetTooltip(_, v)
	if type(v) == "nil" then return self.db.profile.tooltip end

	if self.db.profile.tooltip ~= v then
		self.db.profile.tooltip = v

		self:UpdateAll()
	end
end
function plugin:GetSetSide(_, v)
	if type(v) == "nil" then return self.db.profile.side end

	if self.db.profile.side ~= v then
		self.db.profile.side = v

		self.core:UpdateSideOrder()
		self:UpdateAllWith(function(frame)
			local debuffbar = frame.parts[moduleName]
			local n = 1
			while debuffbar[n] do
				debuffbar[n]:Hide()
				debuffbar[n].t:SetTexture()
				debuffbar[n]:SetScript("OnEnter", nil)
				debuffbar[n]:SetScript("OnLeave", nil)
				debuffbar[n].tfont:SetText()
				debuffbar[n].afont:SetText()
				debuffbar[n] = nil
				n = n + 1
			end
			self.core:UpdateSide(frame)
		end)

		
		self:UpdateAll()
	end
end


--[[
function plugin:GetSetedgeSize(_, v)
	if type(v) == "nil" then return self.db.profile.edgeSize end

	if self.db.profile.edgeSize ~= v then
		
		self.db.profile.edgeSize = v

		self:UpdateAllWith(function(frame)
			for i=1, 8 do
				frame.parts[moduleName]["border"..i]:SetWidth(self.db.profile.edgeSize)
				frame.parts[moduleName]["border"..i]:SetHeight(self.db.profile.edgeSize)
			end
		end)
	end
end
function plugin:GetSetInsets(_, v)
	if type(v) == "nil" then return self.db.profile.insets end

	if self.db.profile.insets ~= v then
		
		self.db.profile.insets = v

		self:UpdateAllWith(function(frame, debuffbar)
			local insets = self.db.profile.insets
			local edgeSize = self.db.profile.edgeSize
			debuffbar.border1:SetPoint("TOPLEFT", frame, "TOPLEFT", -insets, -edgeSize + insets)
			debuffbar.border1:SetPoint("BOTTOMLEFT", frame, "BOTTOMLEFT", -insets, edgeSize - insets)
			debuffbar.border2:SetPoint("TOPRIGHT", frame, "TOPRIGHT", insets, -edgeSize + insets)
			debuffbar.border2:SetPoint("BOTTOMRIGHT", frame, "BOTTOMRIGHT", insets, edgeSize - insets)
			debuffbar.border3:SetPoint("TOPLEFT", frame, "TOPLEFT", edgeSize - insets, insets)
			debuffbar.border3:SetPoint("TOPRIGHT", frame, "TOPRIGHT", -edgeSize + insets, insets)
			debuffbar.border4:SetPoint("BOTTOMLEFT", frame, "BOTTOMLEFT", edgeSize - insets, -insets)
			debuffbar.border4:SetPoint("BOTTOMRIGHT", frame, "BOTTOMRIGHT", -edgeSize + insets, -insets)
			debuffbar.border5:SetPoint("TOPLEFT", frame, "TOPLEFT", -insets, insets)
			debuffbar.border6:SetPoint("TOPRIGHT", frame, "TOPRIGHT", insets, insets)
			debuffbar.border7:SetPoint("BOTTOMLEFT", frame, "BOTTOMLEFT", -insets, -insets)
			debuffbar.border8:SetPoint("BOTTOMRIGHT", frame, "BOTTOMRIGHT", insets, -insets)
		end)
	end
end
--]]
